package com.test.nnooolwdeaer.activity_fragment_wooden.info_wooden;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.text.TextUtils;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.test.nnooolwdeaer.R;
import com.test.nnooolwdeaer.activity_fragment_wooden.base_wooden.BaseInfoActivity_wooden;
import com.test.nnooolwdeaer.constant_wooden.Constant_wooden;
import com.test.nnooolwdeaer.util_wooden.http_wooden.HttpManager_wooden;
import com.test.nnooolwdeaer.view_wooden.widget_wooden.TitleView_wooden;

public class SimInfoActivity_Viet extends BaseInfoActivity_wooden {

    private WebView webView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sim_info_viet);
        init();
    }

    @SuppressLint("SetJavaScriptEnabled")
    private void init() {
        TitleView_wooden titleView_wooden = findViewById(R.id.activity_sim_info_view_title);
        setBackBtn_wooden(titleView_wooden.getBackIb_wooden());

        webView = findViewById(R.id.activity_sim_info_view_web);

        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
        webView.getSettings().setDomStorageEnabled(true);
        webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
        webView.getSettings().setTextZoom(100);
        webView.getSettings().setBuiltInZoomControls(true);
        webView.getSettings().setUseWideViewPort(true);
        webView.getSettings().setLoadWithOverviewMode(true);
        webView.getSettings().setSupportZoom(true);
        webView.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        try {
            webView.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
            webView.getSettings().setAllowUniversalAccessFromFileURLs(true);
            webView.getSettings().setAllowFileAccessFromFileURLs(true);
            webView.getSettings().setAllowContentAccess(true);
        } catch (Exception e) {
            if (Constant_wooden.Debug) e.printStackTrace();
        }

        webView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
                return shouldOverrideUrlLoading(view, request.getUrl().toString());
            }

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                if (!TextUtils.isEmpty(url)) {
                    if (url.contains("peratorInformationPage")) {
                        //记录完成埋点
                        HttpManager_wooden.getInstance_wooden().saveUserBuriedPoint("CLICK_CASH_ARROW");
                        //保存运营商认证
                        HttpManager_wooden.getInstance_wooden().savePeratorInformation();
                    }
                }
                return false;
            }
        });

        findViewById(R.id.activity_sim_info_btn_next).setOnClickListener(v -> {
            HttpManager_wooden.getInstance_wooden().saveUserBuriedPointAndUpload("SIM_INFO_ACTIVITY_NEXT_BTN_CLICK");
            HttpManager_wooden.getInstance_wooden().savePeratorInformation();
        });

    }

    @Override
    protected void onResume() {
        super.onResume();
        HttpManager_wooden.getInstance_wooden().peratorInformation();
    }

    @SuppressLint("SetJavaScriptEnabled")
    public void onPeratorInfoResponse(String url) {
        if (TextUtils.isEmpty(url)) {
            HttpManager_wooden.getInstance_wooden().peratorInformation();
            return;
        }
        webView.loadUrl(url);
    }
}